package dontCare.gwtester.client.puzzlers;

import java.util.ArrayList;
import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;

import dontCare.gwtester.client.tool.SourceUrl;
import dontCare.gwtester.client.tool.UnitWidget;

public class Album4 extends UnitWidget{
	private static Album4UiBinder uiBinder = GWT.create(Album4UiBinder.class);
	interface Album4UiBinder extends UiBinder<Widget, Album4> {}

	@UiField Label bigDelightInEveryByte;
	@UiField Label inclementIncrement;
	@UiField Label inLoop;
	@UiField Label shiffy;
	@UiField Label infinity;
	@UiField Label e40;
	@UiField Label brideLooper;
	@UiField Label ghostLooper;
	@UiField Label curseBiggerThan;
	@UiField Label curseEqual;
	@UiField Label meetWolfman;
	@UiField Label downCount;
	@UiField Label minute;
	
	public Album4() {
		initWidget(uiBinder.createAndBindUi(this));
		bigDelightInEveryByte();
		inclementIncrement();
		inLoop.setText(""+(Integer.MAX_VALUE+1));
		shiffy.setText(""+(-1<<32));
		looper();
		brideLooper.setText(""+(Double.NaN!=Double.NaN));
		ghostLooper();
		curseLooper();
		meetWolfman.setText(""+(Integer.MIN_VALUE == -Integer.MIN_VALUE));
		downCount();
		minute();
	}

	private void bigDelightInEveryByte() {
		StringBuffer result = new StringBuffer();
		for(byte b=Byte.MIN_VALUE; b<Byte.MAX_VALUE; b++){
			if(b==0x90){
				result.append("Joy! ");
			}
		}
		bigDelightInEveryByte.setText(result.toString());
	}

	private void inclementIncrement() {
		int j=0;
		for(int i=0; i<100; i++){
			j = j++;
		}
		inclementIncrement.setText(""+j);
	}
	
	/*
	 * 如果寫「Double.POSITIVE_INFINITY+1 == Double.POSITIVE_INFINITY」
	 * Eclipse 會提示 warning：「Comparing indentical expression」
	 * 有 IDE 真好！ [扭動]
	 */
	private void looper() {
		double i = Double.POSITIVE_INFINITY;
		infinity.setText(""+(i+1 == i));
		
		i=1.0e40;
		e40.setText(""+ (i+1 == i));
	}
	
	private void ghostLooper() {
		byte i = -1;
		ghostLooper.setText(""+(i >>>= 1));
	}

	private void curseLooper() {
		Integer i = new Integer(0);
		Integer j = new Integer(0);
		curseBiggerThan.setText(""+(i>=j));
		curseEqual.setText(""+(i==j));
	}

	private void downCount() {
		final int START = 2000000000;
		int count = 0;
		for(float f = START; f<START+50; f++){
			count++;
		}
		downCount.setText(""+count);
	}
	
	private void minute() {
		int minutes = 0;
		for(int ms=0; ms<60*60*1000; ms++){
			if(ms%60*1000==0){
				minutes++;
			}
		}
		minute.setText(""+minutes);
	}
	
	@Override
	public List<String> getSourceUrl() {
		ArrayList<String> result = new ArrayList<String>();
		result.add(SourceUrl.genJava(Album4.class));
		result.add(SourceUrl.genUiXml(Album4.class));
		return result;
	}

	@Override
	public HTML getDescription() {
		return new HTML(
			"《Java Puzzlers》 Chapter 4 puzzler 集合。" +
			"其中 puzzler 26～33 與無窮迴圈有關，故僅在此測試關鍵點。<br />" +
			"Puzzler 34：Development Mode 結果為 0，詳情參閱 gwt/leaky/Abstraction/FloatUnderflow"
		);
	}

	@Override
	public String getName() {
		return "CH.4 Album";
	}
}